import { LoggerService } from '@nestjs/common';
import dayjs from 'dayjs';

enum MsgType {
  LOG = 'log',
  ERROR = 'error',
  WARN = 'warn',
  DEBUG = 'debug',
  VERBOSE = 'verbose'
};

const MsgColorMap = {
  [MsgType.LOG]: '\x1b[34m[LOG]\x1b[0m',
  [MsgType.ERROR]: '\x1b[31m[ERROR]\x1b[0m',
  [MsgType.WARN]: '\x1b[33m[WARN]\x1b[0m',
  [MsgType.DEBUG]: '\x1b[35m[DEBUG]\x1b[0m',
  [MsgType.VERBOSE]: '\x1b[36m[VERBOSE]\x1b[0m'
};

export class MyLogger implements LoggerService {
  private static readonly APP_NAME = 'Nest';
  private static readonly PROCESS_ID = process.pid;

  private getMsg(message: string, type: MsgType): string {
    const timestamp = dayjs().format('YYYY/MM/DD HH:mm:ss');
    return `${MyLogger.APP_NAME} ${MyLogger.PROCESS_ID} ${timestamp} ${MsgColorMap[type]} ${message}`;
  }

  log(message: string) {
    console.log(`${this.getMsg(message, MsgType.LOG)}`);
  }

  error(message: string, trace: string) {
    console.error(`${this.getMsg(message, MsgType.ERROR)}`, trace);
  }

  warn(message: string) {
    console.warn(`${this.getMsg(message, MsgType.WARN)}`);
  }

  debug(message: string) {
    console.debug(`${this.getMsg(message, MsgType.DEBUG)}`);
  }

  verbose(message: string) {
    console.log(`${this.getMsg(message, MsgType.VERBOSE)}`);
  }
}